package ro.dandanciu.webgallery.domainmodel;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * @author Dan
 *
 */
@Entity
@Table(name="TP_ADVERT")
@NamedQueries ( {
	@NamedQuery(
			name = "Advert.findForUser", 
			query = "select a from Advert a left outer join a.users u where u.userId = :userId")	
}
)
public class Advert {
	
	/**
	 * The identifier of the gallery.
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name="K_ADVERT", length=30)
	private long id;
	
	/**
	 * The name of the gallery.
	 */
	@Column(name="C_TEXT", length=200) 
	private String aText;

	@ManyToOne
	@JoinColumn(name="F_TARGET")
	private Gallery gallery;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getAText() {
		return aText;
	}

	public void setAText(String text) {
		aText = text;
	}
	
	public Gallery getGallery() {
		return gallery;
	}

	public void setGallery(Gallery gallery) {
		this.gallery = gallery;
	}
	
	@ManyToMany
    @JoinTable(name="TP_ADVERT_CLIENTS",
               joinColumns={@JoinColumn(name="FK_ADVERT")},
               inverseJoinColumns={@JoinColumn(name="FK_USER")})
	private Set<User> users;
	
	/**
	 * The timestamp of the last time this item was updated.
	 * Mapped to D_UPDATED.
	 */
	@Column(name="D_UPDATED")
	@Temporal(value=TemporalType.TIMESTAMP)
	private Date updated;
		
	/**
	 * The user id/software which has performed the last update.
	 * Mapped to C_UPDATER.
	 */
	@Column(name="C_UPDATER", length=30) 
	private String updater;

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	public Date getUpdated() {
		return updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}

	public String getUpdater() {
		return updater;
	}

	public void setUpdater(String updater) {
		this.updater = updater;
	}

}
